package it.koutifaris.travianmap.utils;

public class ModularBounds {

	private BoundsSource boundsSource;

	public ModularBounds(final int min, final int max) {
		this.boundsSource = new BoundsSource() {

			@Override
			public int getMin() {
				return min;
			}

			@Override
			public int getMax() {
				return max;
			}
		};
	}

	public ModularBounds(BoundsSource boundsSource) {
		this.boundsSource = boundsSource;
	}

	public int module(int val) {
		return modularCoords(getMin(), getMax(), val);
	}

	private int getMin() {
		return boundsSource.getMin();
	}

	private int getMax() {
		return boundsSource.getMax();
	}

	public static int modularCoords(int min, int max, int val) {
		int width = max - min;
		return min + ((val - min) % (width) + width) % width;
	}

	public interface BoundsSource {
		int getMin();

		int getMax();
	}
}
